﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class DriverScoreDAO
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DriverScore model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DriverScore(");
            strSql.Append("Driver,Score,Type,BussId,CreateTime)");
            strSql.Append(" values (");
            strSql.Append("@Driver,@Score,@Type,@BussId,@CreateTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@Driver", SqlDbType.Int,4),
                    new SqlParameter("@Score", SqlDbType.Decimal,5),
                    new SqlParameter("@Type", SqlDbType.Int,4),
                    new SqlParameter("@BussId", SqlDbType.Int,4),
                    new SqlParameter("@CreateTime", SqlDbType.DateTime),};
            parameters[0].Value = model.Driver;
            parameters[1].Value = model.Score;
            parameters[2].Value = model.Type;
            parameters[3].Value = model.BussId;
            parameters[4].Value = DateTime.Now;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 获得数据列表
        /// <param name="id">评分所对应业务ID</param>
        /// <param name="type">业务类型</param>
        /// </summary>
        public IList<DriverScore> GetList(int id, int type)
        {
            IList<DriverScore> list = new List<DriverScore>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Driver,Score,Type,CreateTime,BussId ");
            strSql.Append(" FROM DriverScore WHERE BussId=@BussId AND Type=@Type");
            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@BussId",SqlDbType.Int),
                new SqlParameter("@Type",SqlDbType.Int)
            };
            param[0].Value = id;
            param[1].Value = type;
            var dt = DbHelperSQL.Query(CommandType.Text, strSql.ToString(), param);
            foreach (DataRow item in dt.Rows)
            {
                list.Add(DataRowToModel(item));
            }
            return list;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DriverScore DataRowToModel(DataRow row)
        {
            DriverScore model = new DriverScore();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Driver"] != null && row["Driver"].ToString() != "")
                {
                    model.Driver = int.Parse(row["Driver"].ToString());
                }
                if (row["Score"] != null && row["Score"].ToString() != "")
                {
                    model.Score = decimal.Parse(row["Score"].ToString());
                }
                if (row["Type"] != null && row["Type"].ToString() != "")
                {
                    model.Type = int.Parse(row["Type"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["BussId"] != null && row["BussId"].ToString() != "")
                {
                    model.BussId = int.Parse(row["BussId"].ToString());
                }
            }
            return model;
        }
    }
}
